<?php
/**
 * Created by PhpStorm.
 * User: cqfut
 * Date: 2019/04/01
 */
require_once ("../../config/path_config.php");
include_once (ROOTDIR."common/base_router.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."front/model/homework_model.php");

class HomeworkRouter extends BaseRouter {
    public function __construct() {
        parent::__construct();
    }

    function getHomeworkList() {
        // 学生ID
        $stuId = BaseRouter::getSession('user_id');
        if ( $stuId == null ) {
            $this->setRspStatus(0, -1,"用户未登录" );
            return;
        }

        $hwstatus = $this->getReqIntVal('hwstatus');
        $problemId = $this->getReqIntVal('pid');
        $pageId = $this->getReqIntVal('pageid');
        if ( $pageId == -1 ) {
            $pageId = 0;
        }


        $beginIdx = $pageId * SysConfig::$PAGESIZE;

        $model = new HomeworkModel();
        // 获取暂存代码数量
        $cnt = $model->getHomeworkCnt($hwstatus, $problemId, $stuId);
        $maxPageId = floor($cnt/SysConfig::$PAGESIZE);
        // 获取暂停代码信息
        $hwList = $model->getHomeworkList($hwstatus, $problemId, $stuId, $beginIdx, SysConfig::$PAGESIZE);
        $this->setRspVal('homework_list', $hwList);
        $this->setRspVal('max_pageid', $maxPageId);
    }

    function getStashCodeDetail() {
        $createrId = BaseRouter::getSession('user_id');
        if ( $createrId == null ) {
            $this->setRspStatus(0, -1,"用户未登录" );
            return;
        }

        $md5Id = $this->getReqVal('id');
        if ( $md5Id == null ) {
            $this->setRspStatus(0, -2,"参数错误" );
            return;
        }

        $model = new StashCodeModel();
        $codeDetail = $model->getStashCodeDetail($md5Id, $createrId);
        $this->setRspData($codeDetail);
    }

    function getHomeworkDetail() {
        $createrId = BaseRouter::getSession('user_id');
        if ( $createrId == null ) {
            $this->setRspStatus(0, -1,"用户未登录" );
            return;
        }

        $args = array();
        array_push($args, 'title', 'source');
        // 必要参数判断
        if ( ! $this->checkParam($args) ) {
            $this->setRspStatus(0, -2,"缺少必要字段" );
            return;
        }

        $problemId = $this->getReqVal('problem_id');
        if ( $problemId == null ) {
            $problemId = 0;
        }
        $title = $this->getReqVal('title');
        // 无需url解码,php会自动进行url解码
        $source = $this->getReqVal('source');

        // 计算源码ID,采用md5作为源码的唯一标识
        $md5Id = md5($source.$createrId);

        $model = new StashCodeModel();

        // 判断代码是否已存在
        if ( $model->isExist($md5Id) > 0 ) {
            $this->setRspStatus(0, -1,"已存在相同的代码" );
            return;
        }

        $ret = $model->addStashCode($md5Id, $problemId, $title, $source,$createrId);
        if ( $ret >= 0 ) {
            $this->setRspStatus(1, 0,"添加代码成功" );
        }
        else {
            $this->setRspStatus(0, -1,"数据库错误" );
        }
    }

    public function run() {
        $op = $this->getReqVal('op');
        if ( $op == 'list' ) {
            $this->getHomeworkList();
        }
        else if ( $op == 'fetch' ) {
            $this->getHomeworkDetail();
        }
    }
}

$router = new HomeworkRouter();
$router->init();
$router->run();
$router->finish();